<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\api\permissions;

use App\Common\Repositories\System\CustomerRolesRepository;
use App\Model\CustomerRoleHasPermissions;
use Psr\Http\Message\ResponseInterface;
use xsl\basic\BaseController;

class Roles extends BaseController
{
    protected $repository;

    public function __construct(CustomerRolesRepository $repository)
    {
        $this->repository = $repository;
    }

    public function index(): ResponseInterface
    {
        $where = $this->request->all();
        $list = $this->repository->getList($where);
        return $this->success($list);
    }

    public function create(): ResponseInterface
    {
        $data = $this->request->all();
        if (! isset($data['data_range'])) {
            $data['data_range'] = 0;
        } else {
            $data['data_range'] = (int) $data['data_range'];
        }
        $permissions = $data['permissions'];
        if (isset($data['permissions'])) unset($data['permissions']);
        $get_id = $this->repository->insertGetId($data);
        $new_arr = [];
        foreach ($permissions as $permission){
            $new_arr[] = [
                'role_id' => $get_id,
                'permission_id' => $permission
            ];
        }
        CustomerRoleHasPermissions::insert($new_arr);
        return $this->success();
    }

    public function read($id): ResponseInterface
    {
        $where['id'] = $id;
        if ($this->request->has('from') && $this->request->input('from') == 'parent_role') {
            $role = $this->repository->getfirst($where);
        } else {
            $role = $this->repository->getWhere($where, ['*']);
            $role->permissions = CustomerRoleHasPermissions::where("role_id", $id)->pluck("permission_id");
        }
        return $this->success($role);
    }

    public function delete($id): ResponseInterface
    {
        $this->repository->delete($id);
        return $this->success();
    }

    public function update($id): ResponseInterface
    {
        $data = $this->request->all();
        if (! isset($data['data_range'])) {
            $data['data_range'] = 0;
        } else {
            $data['data_range'] = (int) $data['data_range'];
        }
        $data['updated_at'] = time();
        $permissions = $data['permissions'];
        if (isset($data['permissions'])) unset($data['permissions']);
        $new_arr = [];
        foreach ($permissions as $permission){
            $new_arr[] = [
                'role_id' => $id,
                'permission_id' => $permission
            ];
        }
        CustomerRoleHasPermissions::where("role_id", $id)->delete();
        CustomerRoleHasPermissions::insert($new_arr);
        return $this->success($this->repository->update($id, $data));
    }
}
